<?php

namespace app\admin\model;

use think\Model;

class Personnel extends Model
{
    // 表名
    protected $name = 'personnel';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'role_text',
        'sexlist_text',
        'is_class_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getRoleList()
    {
        return ['1' => __('校长'),'2' => __('副校长'),'3' => __('教导主任'),'4' => __('学科备课组长'),'5' => __('年级组长'),'6' => __('班主任'),'7' => __('任课教师'),'8' => __('班长')];
    }     

    public function getSexlistList()
    {
        return ['1' => __('男'),'2' => __('女'),'3' => __('其他')];
    }     

    public function getIsClassList()
    {
        return ['1' => __('Is_class 1'),'2' => __('Is_class 2')];
    }     


    public function getRoleTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['role'];
        $list = $this->getRoleList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSexlistTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['sexlist'];
        $list = $this->getSexlistList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsClassTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['is_class'];
        $list = $this->getIsClassList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function school()
    {
        return $this->belongsTo('School', 'school_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function grade()
    {
        return $this->belongsTo('Grade', 'grade_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function classes()
    {
        return $this->belongsTo('Classes', 'classes_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
